require(["config"], function() {
	require(["jquery", "template", "header", "zoom", "fly", "carousel", "cookie"], function($, template) {
		function Detail() {
			this.renderImg();


		}
		Detail.prototype = {
			constructor: Detail,
			renderImg: function() {
				$.ajax("http://rap2api.taobao.org/app/mock/87266/api/list")
					.done((data) => {
						//待渲染数据
						var data = {
							img: data.res_body.img
						};
						//渲染
						var html = template("pre_template", data);
						$(".preview-wrap").html(html);
						this.zoom();
						this.renderIntro();
						this.addListener();
					})
			},
			//放大镜
			zoom: function() {
				$("#middle-zoom").elevateZoom({
					gallery: 'gal1',
					cursor: 'pointer',
					galleryActiveClass: 'active'
				});
			},
			renderIntro: function() {
				$.ajax("http://rap2api.taobao.org/app/mock/87266/api/list")
					.done(function(data) {
						var data = {
							list: data.res_body.list
						};
						var html = template("intro_template", data);
						$("#introTitle").html(html);
					})
			},
			addListener: function() {
				$("#ctb").on("click", "span", $.proxy(this.clickTitle, this))
				$("#favor").on("click", ".img", $.proxy(this.clickFover, this))
				$(".add-basket").click(this.addToCartHandler);
			},
			clickTitle: function(event) {
				var kinds = event.target;
				console.log(kinds);
				var child = $(kinds).siblings();
				console.log(child);
				var bang = child.text();
				console.log(bang);
				if (bang === "2.2磅3.2磅5.2磅") {
					$("#price").html("198");
				} else if (bang === "1.2磅3.2磅5.2磅") {
					$("#price").html("298");
				} else if (bang === "1.2磅2.2磅5.2磅") {
					$("#price").html("438");
				} else if (bang === "1.2磅2.2磅3.2磅") {
					$("#price").html("728");
				}
				$(child).removeClass("active");
				$(kinds).addClass("active");
				this.renderIntro();
			},
			addToCartHandler: function(event) {

				// 获取当前选购商品的信息
				var currentProduct = {
					id: $(".prod_id").text(),
					cn: $(".cn").text(),
					en:$(".en").text(),
					img: $("#gal1 img:first").attr("src"),
					price: $("#price").text(),
					amount: 1
				};
				console.log(currentProduct);
				/* 判断是否已选购过当前商品 */
				// 从 cookie 中读取以有的购物车数据
							$.cookie.json = true; // 配置自动在JS值与JSON文本之间相互转换
							var products = $.cookie("cart") || [];
							// 判断是否已有选购
							var has = products.some(function(prod) {
								if (prod.id == currentProduct.id) { // 已有选购商品
									prod.amount++; // 数量自增
									return true;
								}
								return false;
							});
							if (!has) // 未选购
								products.push(currentProduct);
			
							// 保存购物车：存回cookie
							// 数据结构：[{id,title,img,price,amout},{id,title,img,price,amout},{id,title,img,price,amout}]
							$.cookie("cart", products, {
								expires: 10,
								path: "/"
							});
							console.log($(".popup-info").html("您所选的商品已加入购物篮！"));
							$("#maskPopup").attr("style","display:block;")
							$(".popups").attr("style","display:block;")
							$(".btn-cancel").on("click",function(){
								$("#maskPopup").attr("style","display:none;")
							})
							$(".btn-sure").on("click",function(){
								$(".btn-sure").attr("href","/html/cart.html")
							})
							
			}
		}
		new Detail();
	});
});
